<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>UUID Object Test / UUID.js</title>
    <script type="text/javascript" src="../src/uuid.js"></script>
  </head>
  <body>
    <h1>UUID Object Test / UUID.js</h1>
    <script type="text/javascript">
      // <![CDATA[
      var n = 32;
      var names = ["timeLow", "timeMid", "timeHiAndVersion", "clockSeqHiAndReserved", "clockSeqLow", "node"];

      var messages = [];
      var uuids = [];

      // test UUID.parse() and UUID#equals()
      for (var i = 0; i < n; i++) {
        var lft = (i & 1) ? UUID.genV1() : UUID.genV4();
        var rgt = (i & 1) ? UUID.genV4() : UUID.genV1();

        var parsed = UUID.parse(i & 2 ? lft : lft.urn);
        if (parsed == null) {
          messages.push("UUID.parse() error\t" + lft);
        } else {
          if (parsed.version != lft.version) {
            messages.push("UUID.parse()#version error\t" + parsed + ", " + lft);
          }

          if (!lft.equals(parsed) || !parsed.equals(lft) || rgt.equals(parsed) || parsed.equals(rgt)) {
            messages.push("UUID#equals() error\t" + lft + ", " + rgt + ", " + parsed);
          }
        }

        uuids.push(lft, rgt, parsed);
      }

      // test UUID.parse() in another way
      for (var i = 0, len = uuids.length; i < len; i++) {
        var uuid = uuids[i];

        var valid = [uuid.urn, uuid.hexString.toUpperCase(), "{" + uuid + "}",
                     " " + uuid + "\n", uuid.urn.toUpperCase()];
        for (var j = 0; j < valid.length; j++) {
          var parsed = UUID.parse(valid[j]);
          if (parsed == null) {
            messages.push("UUID.parse() error (type 1)\t" + valid[j]);
          }
        }

        var invalid = ["{" + uuid, uuid + "}", uuid.urn + "}", "{ " + uuid + "}"];
        for (var j = 0; j < invalid.length; j++) {
          var parsed = UUID.parse(invalid[j]);
          if (parsed != null) {
            messages.push("UUID.parse() error (type 2)\t" + invalid[j]);
          }
        }
      }

      // test formatted strings, field values and version number
      for (var i = 0, len = uuids.length; i < len; i++) {
        var uuid = uuids[i];

        var bstr = uuid.bitString;
        var bfld = [bstr.substring(0, 32),  bstr.substring(32, 48), bstr.substring(48, 64),
                    bstr.substring(64, 72), bstr.substring(72, 80), bstr.substring(80, 128)];
        var hstr = uuid.hexString;
        var hfld = [hstr.substring(0, 8),   hstr.substring(9, 13),  hstr.substring(14, 18),
                    hstr.substring(19, 21), hstr.substring(21, 23), hstr.substring(24, 36)];

        for (var j = 0; j < 6; j++) {
          var nm = names[j];
          if (uuid.intFields[j] != uuid.intFields[nm]) {
            messages.push("UUID#intFields[" + j + " or " + nm + "] error\t" + uuid);
          }
          if (uuid.bitFields[j] != uuid.bitFields[nm] || uuid.intFields[j] != parseInt(uuid.bitFields[j], 2)) {
            messages.push("UUID#bitFields[" + j + " or " + nm + "] error\t" + uuid);
          }
          if (uuid.hexFields[j] != uuid.hexFields[nm] || uuid.intFields[j] != parseInt(uuid.hexFields[j], 16)) {
            messages.push("UUID#hexFields[" + j + " or " + nm + "] error\t" + uuid);
          }

          if (bfld[j] != uuid.bitFields[j]) {
            messages.push("UUID#bitString error\t" + uuid);
          }
          if (hfld[j] != uuid.hexFields[j]) {
            messages.push("UUID#hexString error\t" + uuid);
          }
        }

        var version = parseInt(bstr.substring(48, 52), 2);
        if (version != uuid.version) {
          messages.push("UUID#version error\t" + uuid);
        }

        if (uuid.urn != "urn:uuid:" + uuid.hexString) {
          messages.push("UUID#urn error\t" + uuid);
        }
      }

      if (messages.length == 0) {
        messages.push("No error was detected.");
      }
      // ]]>
    </script>
    <h2>Test Messages</h2>
    <pre><script type="text/javascript">document.writeln(messages.join("\n"));</script></pre>
    <h2>Generated UUIDs</h2>
    <pre><script type="text/javascript">document.writeln(uuids.join("\n"));</script></pre>
  </body>
</html>
